package com.example.bobo.yuejian322.Fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.bobo.yuejian322.Adapter.ShaituAdapter;
import com.example.bobo.yuejian322.Interfa.ShaituSeverice;
import com.example.bobo.yuejian322.R;
import com.example.bobo.yuejian322.base.BaseFragment;
import com.example.bobo.yuejian322.bean.Shaitu;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class SaituFragment extends BaseFragment {

    private RecyclerView gridView;
    private ShaituAdapter adapter;
    private  String shaitu="shaitu";
    private ImageView dialog;

    public SaituFragment() {
        // Required empty public constructor
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        gridView = (RecyclerView) view.findViewById(R.id.gridview);
        dialog= (ImageView) view.findViewById(R.id.imageButton_xiala);

//监听
        dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            }
        });

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175/workout/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();


        ShaituSeverice shaituSeverice =retrofit.create(ShaituSeverice.class);
        Call<List<Shaitu>> Listcall=shaituSeverice.shaituList(getUserId(),shaitu,1);
        Listcall.enqueue(new Callback<List<Shaitu>>() {
            @Override
            public void onResponse(Call<List<Shaitu>> call, Response<List<Shaitu>> response) {
                List<Shaitu> shaituList = response.body();

                if (getActivity() != null) {
                    adapter = new ShaituAdapter(getActivity(),shaituList);
                    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    gridView.setLayoutManager(new StaggeredGridLayoutManager(2,
                            StaggeredGridLayoutManager.VERTICAL));
                    gridView.setAdapter(adapter);
                    gridView.setItemAnimator(new DefaultItemAnimator());

                }

            }

            @Override
            public void onFailure(Call<List<Shaitu>> call, Throwable t) {

            }
        });



    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_saitu, container, false);
    }

}
